package sy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.TOrderCash;
import sy.model.Taccount;
import sy.model.TcustomerInfo;
import sy.pageModel.DataGrid;
import sy.pageModel.PageOrderCash;
import sy.service.TOrderCashService;
import sy.service.TaccountServiceI;
import sy.service.TcustomerInfoServiceI;
import sy.util.DateUtil;

/**
 * 
 * @author ray
 * 
 */
@Service("tOrderCashService")
public class TOrderCashServiceImpl implements TOrderCashService {

	private BaseDaoI<TOrderCash> baseDao;

	public BaseDaoI<TOrderCash> getBaseDao() {
		return baseDao;
	}

	TaccountServiceI taccountService;

	TcustomerInfoServiceI tcustomerInfoService;

	public TaccountServiceI getTaccountService() {
		return taccountService;
	}

	@Autowired
	public void setTaccountService(TaccountServiceI taccountService) {
		this.taccountService = taccountService;
	}

	public TcustomerInfoServiceI getTcustomerInfoService() {
		return tcustomerInfoService;
	}

	@Autowired
	public void setTcustomerInfoService(
			TcustomerInfoServiceI tcustomerInfoService) {
		this.tcustomerInfoService = tcustomerInfoService;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<TOrderCash> baseDao) {
		this.baseDao = baseDao;
	}

	public PageOrderCash add(PageOrderCash pageOrderCash) {
		TOrderCash tOrderCash = new TOrderCash();
		Taccount taccount = this.taccountService.get(pageOrderCash.getAccno());
		TcustomerInfo tcustomerInfo = this.tcustomerInfoService.get(pageOrderCash.getTcustomerInfoId());
		tOrderCash.setTaccount(taccount)
				  .setTcustomerInfo(tcustomerInfo)
				  .setAcccurr(pageOrderCash.getAcccurr())
				  .setAccname(taccount.getAccName())
				  .setAccno(taccount.getAccNo())
				  .setPrice(pageOrderCash.getPrice())
				  .setDescription(pageOrderCash.getDescription())
				  .setUnits(pageOrderCash.getUnits())
				  .setCreatetime(DateUtil.format(new Date()));
		tOrderCash.setCashid(UUID.randomUUID().toString());
		this.baseDao.save(tOrderCash);
		return pageOrderCash;
	}

	public PageOrderCash edit(PageOrderCash pageOrderCash) {
		TOrderCash tOrderCash = this.baseDao.get(TOrderCash.class, pageOrderCash.getCashid());
		Taccount taccount = this.taccountService.get(pageOrderCash.getAccno());
		TcustomerInfo tcustomerInfo = this.tcustomerInfoService.get(pageOrderCash.getTcustomerInfoId());
		if (null != tOrderCash) {
			tOrderCash.setTaccount(taccount)
			  .setTcustomerInfo(tcustomerInfo)
			  .setAcccurr(pageOrderCash.getAcccurr())
			  .setAccname(taccount.getAccName())
			  .setAccno(taccount.getAccNo())
			  .setPrice(pageOrderCash.getPrice())
			  .setDescription(pageOrderCash.getDescription())
			  .setUnits(pageOrderCash.getUnits());
			this.baseDao.update(tOrderCash);
		}
		return pageOrderCash;
	}

	public void remove(String ids) {
		for (String i : ids.split(",")) {
			TOrderCash tOrderCash = this.baseDao.get(TOrderCash.class, i);
			if (null != tOrderCash) {
				this.baseDao.delete(tOrderCash);
			}
		}
	}

	public DataGrid treegrid() {
		DataGrid dataGrid = new DataGrid();
		String hql = "from TOrderCash t ";
		List<PageOrderCash> pageOrderCashs = new ArrayList<PageOrderCash>();
		List<TOrderCash> orderCashs = this.baseDao.find(hql);
		PageOrderCash pageOrderCash = null;
		if (null != orderCashs && orderCashs.size() > 0) {
			for (TOrderCash tOrderCash : orderCashs) {
				pageOrderCash = new PageOrderCash();
				BeanUtils.copyProperties(tOrderCash, pageOrderCash);
				pageOrderCash.setTcustomerInfoId(tOrderCash.getTcustomerInfo().getCustomerid());
				pageOrderCash.setCustomerzhhame(tOrderCash.getTcustomerInfo().getCustomerzhname());
				pageOrderCashs.add(pageOrderCash);
			}
		}
		dataGrid.setRows(pageOrderCashs);
		dataGrid.setTotal(this.baseDao.count("select count(*) " + hql));
		return dataGrid;
	}

}
